/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TestApplication;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.io.FileInputStream;

/**
 *
 * @author ba942
 */
public class Main {

    /**
     * Egy ASCII art-os logo mindig kihagyhatatlan egy konzolos programbol,
     * ezt a nemes feladatot latja el a fuggveny.
     */
    public static void csicsa(){
        System.out.println(
                "\n"+
"                  _____ _____ _____ _____ _______       _      \n"+
"                 |  __ \\_   _/ ____|_   _|__   __|/\\   | |     \n"+
"                 | |  | || || |  __  | |    | |  /  \\  | |     \n"+
"                 | |  | || || | |_ | | |    | | / /\\ \\ | |     \n"+
"                 | |__| || || |__| |_| |_   | |/ ____ \\| |____ \n"+
"                 |_____/_____\\_____|_____|  |_/_/    \\_\\______|\n"+
"           _____ _____ __  __ _    _ _            _______ ____  _____  \n"+
"          / ____|_   _|  \\/  | |  | | |        /\\|__   __/ __ \\|  __ \n"+
"         | (___   | | | \\  / | |  | | |       /  \\  | | | |  | | |__) |\n"+
"          \\___ \\  | | | |\\/| | |  | | |      / /\\ \\ | | | |  | |  _  / \n"+
"          ____) |_| |_| |  | | |__| | |____ / ____ \\| | | |__| | | \\ \\ \n"+
"         |_____/|_____|_|  |_|\\____/|______/_/    \\_\\_|  \\____/|_|  \\_\\ \n"+
"                                            ___      ___                   \n"+
"              ___      __      ___        //   ) ) //   ) ) /_  / /_  /    \n"+
"            //   ) ) //  ) ) //   ) )      ___/ / //   / /   / /   / /     \n"+
"           //___/ / //      //   / /     / ____/ //   / /   / /   / /      \n"+
"          //       //      ((___/ /     / /____ ((___/ /   / /   / /      \n"+
"\n"+
"                            ULTIMA PRIME EDITION                            "+
"\n"+
"       ______________________ _________________________________________ \n"+
"       \\__    ___/\\_   _____//   _____/\\__    ___/\\_   _____/\\______   \\\n"+
"         |    |    |    __)_ \\_____  \\   |    |    |    __)_  |       _/\n"+
"         |    |    |        \\/        \\  |    |    |        \\ |    |   \\\n"+
"         |____|   /_______  /_______  /  |____|   /_______  / |____|_  /\n"+
"                          \\/        \\/                    \\/         \\/ \n"+
"\n");
    }

    public static boolean runTest(File infile, File outfile) throws IOException
    {
        File tempfile=File.createTempFile("DSP", null);
        Application.Main.main(new String []{"NOT FROM COMMANDLINE",infile.getPath(),tempfile.getPath()});
        System.out.print("=>Teszt "+infile.getName()+" : ");
        Scanner  fil1=new Scanner(new FileInputStream (outfile.getPath()));
        Scanner  fil2=new Scanner(new FileInputStream (tempfile.getPath()));
        Boolean siker=true;
        int sorszam=0;
        while(fil1.hasNextLine()){
            String sor1=fil1.nextLine();
            ++sorszam;
            if(sor1.isEmpty())
                continue;
            if (!fil2.hasNextLine()){
                System.out.println("[HIBA] A kimenet kevesebb sort tartalmaz, mint kene");
                siker=false;
                break;
            }
            
            String sor2=fil2.nextLine(); 
            
            if (!sor2.matches("^"+sor1+"$")){
                System.out.println("[HIBA] Hibas kimeneti sor ("+sorszam+"):");
                System.out.println("Elvart kimenet: "+sor1);
                System.out.println("Kapott kimenet: "+sor2);
                siker=false;
                break;
            }
        }
        if (siker)
            System.out.println("[OK]");
        fil1.close();
        fil2.close();
        tempfile.delete();
        return siker;
    }

    /**
     * Megkeresi az adott konyvtarakban az osszes .in .out filepart, illetve
     * rekurzivan meghivja magat az alkonyvtarakra.
     * Meghivasa: findTests(new File("konyvtarnev"))
     * @param dir
     */
    public static boolean findTests(File dir)
    {
        boolean result=true;
        for(File fil:dir.listFiles())
        if (!fil.isHidden()){
            if(fil.isDirectory())                
                result&=findTests(fil);
            else
            if (fil.getName().endsWith(".in")){

                    File outfil=new File(fil.getPath().replace(".in", ".out"));
                    if (outfil.exists())
                    try{
                        result&=runTest(fil,outfil);
                    }catch(IOException ex){
                        System.out.println("IO fail:"+ex);
                    }
            }
        }
        return result;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        csicsa();
        if(findTests(new File("testfiles")))
            System.out.println(
                    " __             _           __\n"+
                    "| _|       ___ | | __      |_ |\n"+
                    "| |       / _ \\| |/ /       | |\n"+
                    "| |      | (_) |   <        | |\n"+
                    "| |       \\___/|_|\\_\\       | |\n"+
                    "|__|                       |__|\n"
                    );
        else
            System.out.println(
                    " __   _     _ _             __\n"+
                    "| _| | |__ (_) |__   __ _  |_ |\n"+
                    "| |  | '_ \\| | '_ \\ / _` |  | |\n"+
                    "| |  | | | | | |_) | (_| |  | |\n"+
                    "| |  |_| |_|_|_.__/ \\__,_|  | |\n"+
                    "|__|                       |__|\n"
                    );
    }

}
